package com.example.opengldemo._00_real_light;

import android.opengl.GLES30;
import android.view.View;

import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;

class Obj_Vertex_Normal_Texcoors_Demo {

    String vertexShaderPath = "z_real_light/reflection_vertex_cube.sh";
    String fragShaderPath = "z_real_light/reflection_frag_cube.sh";

    public ShaderHandle shaderHandle;
    public ShaderBuffer shaderBuffer;

    private float[] vertexArray = null;
    private float[] normalArray = null;
    private float[] texcoorsArray = null;

    public Obj_Vertex_Normal_Texcoors_Demo(View view, float[] vertexArray, float[] normalArray, float[] texcoorsArray) {
        this.vertexArray = vertexArray;
        this.normalArray = normalArray;
        this.texcoorsArray = texcoorsArray;
        initProgram(view);
        initBuffer();
    }

    private void initProgram(View view) {
        String vertexShaderRes = ShaderUtil.loadFromAssetsFile(vertexShaderPath, view.getResources());
        String fragShaderRes = ShaderUtil.loadFromAssetsFile(fragShaderPath, view.getResources());
        initHandle(ShaderUtil.createProgram(vertexShaderRes, fragShaderRes));
    }

    private void initHandle(int mProgram) {
        shaderHandle = new ShaderHandle(mProgram);
    }

    private void initBuffer() {
        shaderBuffer = new ShaderBuffer();
        shaderBuffer.setVertexBuffer(getVertexArray());
        shaderBuffer.setNormalBuffer(getNormalArray());
        shaderBuffer.setTexcoorBuffer(getTexcoorsArray());
    }

    private float[] getVertexArray() {
        return vertexArray;
    }

    private float[] getNormalArray() {
        return normalArray;
    }

    private float[] getTexcoorsArray() {
        return texcoorsArray;
    }

    public void drawSelf(int textureid) {
        GLES30.glUseProgram(shaderHandle.getProgram());
        shaderHandle.program().bindCameraHandle().bindMMaxtrixHandle().bindMVPMaxtrixHandle()
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindNormalHandle(shaderBuffer.getNormalBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindDefault3DTextureHandle(textureid)
                .run(GLES30.GL_TRIANGLES , shaderBuffer.vCount);
    }
}
